/**
 * $Id: BatchDataSubDsmpInterface.java,v 1.3 2006/08/07 08:24:59 star Exp $
 *
 * Copyright 2006 Skyinfo, Inc. All Rights Reserved.
 */
package com.haoxi.subdsmp;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;

import com.haoxi.gateway.util.db.DBConnectUtil;

/**
 * TODO
 * 
 * Revision History
 *
 * 2006-7-31,star.Zhao,created it
 */
public class BatchDataSubDsmpInterface implements Runnable {

    private static int seq = 20000;
    /**
     * 需要推定的批量数据
     */
    String subDsmpData[][] = 
    { 
            
    };
    /**
     * @param args
     */
    public static void main(String[] args) {
        // TODO Auto-generated method stub
        System.out.println("----------------------------start-------------------");
        BatchDataSubDsmpInterface interface1 = new BatchDataSubDsmpInterface();
        Thread thread = new Thread(interface1);
        thread.start();
        System.out.println("----------------------------end-------------------");
    }
    
    /**
     * 取消用户订制的业务--外部取消，发送反向取消的消息给MISC。数据库表中相应的订购关系会在MISC平台返回正向取消的请求时消除。
     * 所以外部取消无需修改数据库状态。
     * @param mobileID    手户手机号码
     * @param serviceID   业务代码
     * @param serviceType 业务类型
     * @param ID          用户ID
     */
    public void externalCancel(List list) {
            
//            String sendAddr_deviceType = "400";
//            String sendAddr_deviceID = "919450";
//            String destAddr_deviceType = "0";
//            String destAddr_deviceID = "0011";
//            String feeIDType = "1";
//            String feeMobile = mobile_ID;
//            String feePseudoCode = "";
//            String destIDType = "1";
//            String destMobile = mobile_ID;
//            String destPseudoCode = "";
//            String serviceIDType = "1";
//            String serviceID = service_ID;
//            String featureMobile = "";
//            String featureContent = "";
//            String isDeal = "-1";
//            String gatewayCode = "1860";
//            String spID = "919450";
            String sSQL =  
              " insert into sms.subscribereq "+
              " (id,msgtype,transactionid,sendaddr_devicetype,sendaddr_deviceid,destaddr_devicetype,destaddr_deviceid,"+
              "  feeidtype,feemobile,feepseudocode,destidtype,destmobile,destpseudocode,serviceidtype,serviceid,"+
              "  featuremobile,featurecontent,isdeal,gatewaycode,savetime,spid)"+
              " values"+
              " (sms.sms_subscribereq_id_seq.nextval,'UnSubscribeServiceReq', ? ,'400','919450', '0','0011',1,?,'',1,?,'','1',?,'','',-1,"+
              "  '1860',sysdate,'919450')";
            
            System.out.println("BatchDataSubDsmpInterface size : [" + list.size() + "]");
            if (list.size() > 0){
            // xxx
            Connection con = null;
            PreparedStatement ps = null;
            try {
                con = DBConnectUtil.open("sms");
                System.out.println("BatchDataSubDsmpInterface sql : [" + sSQL + "]");
                ps = con.prepareStatement(sSQL);
                if (null == subDsmpData){
                    throw new IllegalArgumentException();
                }
                int length = subDsmpData.length;
                Iterator iterator = list.iterator();
                while (iterator.hasNext()){
                    Bean bean = (Bean)iterator.next();
                    String transactionID = "919450"
                        + new java.text.DecimalFormat("0000000000").format(seq++);
                    ps.setString(1, transactionID);
                    ps.setString(2, bean.getMobile());
                    ps.setString(3, bean.getMobile());
                    ps.setString(4, bean.getSeviceID());
                    ps.addBatch();
                }
                for (int i = 0; i < length; i++){
                    String transactionID = "919450"
                        + new java.text.DecimalFormat("0000000000").format(seq++);
                    ps.setString(1, transactionID);
                    ps.setString(2, subDsmpData[i][0]);
                    ps.setString(3, subDsmpData[i][0]);
                    ps.setString(4, subDsmpData[i][1]);
                    ps.addBatch();
                }
                
                ps.executeBatch();
//                if (length != res.length){
//                    throw new SQLException("excute batch failure!!!");
//                }
            } catch (SQLException e) {
                e.printStackTrace();
            }
            finally{
                if (ps != null){
                    try {
                        ps.close();
                        ps = null;
                    } catch (SQLException e) {
                        // TODO Auto-generated catch block
                        e.printStackTrace();
                    }
                }
                
                if (null != con){
                    try {
                        con.close();
                        con = null;
                    } catch (SQLException e) {
                        // TODO Auto-generated catch block
                        e.printStackTrace();
                    }
                } 
            }            
        }
    }

    public List getNeedDeaLData() {
        List result = new ArrayList();
        String sSQL = "select t.mobile , t.serviceid from APP.SILENCE0608016 t where rownum <= 100";
        String sDelete = "delete from APP.SILENCE0608016 t where t.mobile = ? and t.serviceid = ?";
        Connection con = null;
        PreparedStatement ps = null;
        PreparedStatement psdelte = null;
        ResultSet rs = null;
        Bean bean = null ;
        try {
            con = DBConnectUtil.open("sms");
            System.out.println("BatchDataSubDsmpInterface sql : [" + sSQL + "]");
            ps = con.prepareStatement(sSQL);
            psdelte = con.prepareStatement(sDelete);
            rs  = ps.executeQuery();
            while (rs.next()){
                bean = new Bean();
                bean.setMobile(rs.getString(1));
                bean.setSeviceID(rs.getString(2));
                psdelte.setString(1,bean.getMobile());
                psdelte.setString(2,bean.getSeviceID());
                psdelte.addBatch();
                result.add(bean);
            }
            
            System.out.println("delete size:" + psdelte.executeBatch());
            
        } catch (SQLException e) {
            e.printStackTrace();
        }
        finally{
            if (ps != null){
                try {
                    ps.close();
                    ps = null;
                } catch (SQLException e) {
                    // TODO Auto-generated catch block
                    e.printStackTrace();
                }
            }
            
            if (psdelte != null){
                try {
                    psdelte.close();
                    psdelte = null;
                } catch (SQLException e) {
                    // TODO Auto-generated catch block
                    e.printStackTrace();
                }
            }
            if (null != con){
                try {
                    con.close();
                    con = null;
                } catch (SQLException e) {
                    // TODO Auto-generated catch block
                    e.printStackTrace();
                }
            } 
        }
        return result;
    }
    
    public void run() {
        while (true){
            List list = getNeedDeaLData();
            System.out.println("------------------------list size:" + list.size());
            externalCancel(list);
        }
    }
    
}

class Bean {
    private String mobile;
    private String seviceID;
    /**
     * @return Returns the mobile.
     */
    public String getMobile() {
        return mobile;
    }
    /**
     * @param mobile The mobile to set.
     */
    public void setMobile(String mobile) {
        this.mobile = mobile;
    }
    /**
     * @return Returns the seviceID.
     */
    public String getSeviceID() {
        return seviceID;
    }
    /**
     * @param seviceID The seviceID to set.
     */
    public void setSeviceID(String seviceID) {
        this.seviceID = seviceID;
    }
}
